package com.ccrfid.rmc6.resource.business.nightpatrol;

import com.ccrfid.rmc6.appconfig.auth.Secured;
import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.business.nightpatrol.*;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.business.nightpatrol.NightPatrolService;
import com.ccrfid.rmc6.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Secured
@Path("np")
@Produces(MediaType.APPLICATION_JSON)
public class NightPatrolResource {

    @Autowired
    private NightPatrolService nightPatrolService;
//        //获取全部巡更组
//        @GET
//        @Path("groups")
//        public List<NpGroupEntity> getAllNpGroups() {
//            return nightPatrolService.getAllPcGroups();
//        }

    //获取巡更组下属成员
    @GET
    @Path("getPatrolManByGroupId/{groupId}")
    public List<NpGroupMemberEntity> getAllNpgroupMembersByGroupId(@PathParam("groupId") Long groupId) {
        return nightPatrolService.getAllGroupMembersByGroupId(groupId);
    }

    //新增一个巡更组
    @POST
    @Path("AddGroup")
    @Consumes(MediaType.APPLICATION_JSON)
    public Long createNpGroupInfo(NpGroupEntity npGroupEntity) throws AppException {
        return nightPatrolService.createNpGroup(npGroupEntity);
    }

    //新增巡更组成员
    @POST
    @Path("AddPatrolManByGroupId/{groupId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean createNpGroupMemberInfo(@PathParam("groupId") Long groupId, NpGroupMemberEntity npGroupMemberEntity) throws AppException {

        return nightPatrolService.createNpGroupMemberInfo(npGroupMemberEntity, groupId);
    }

    //查询所有的巡更组信息
    @GET
    @Path("getAllGroups")
    public Page<NpGroupViewEntity> getNpRecordPage(@QueryParam("groupName") String groupName,
                                                   @QueryParam("personnelName") String personnelName,
                                                   @QueryParam("patrolPlace") String patrolPlace,
                                                   @QueryParam("patrolTime") String patrolTime,
                                                   @BeanParam PageParam pageParam) {

        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (groupName != null) {
            criteria.add(new Criterion("groupName", Criterion.Operator.LIKE, '%' + groupName + '%'));
        }
        if (personnelName != null) {
            criteria.add(new Criterion("personnelName", Criterion.Operator.LIKE, '%' + personnelName + '%'));
        }
        if (patrolPlace != null) {
            criteria.add(new Criterion("patrolPlace", Criterion.Operator.LIKE, '%' + patrolPlace + '%'));
        }

        if (patrolTime != null) {
            String[] time = patrolTime.split(",");
            java.util.Date startTime = DateUtil.parseDateStr(time[0].toString());
            java.util.Date endTime = DateUtil.parseDateStr(time[1].toString());
            criteria.add(new Criterion("startTime", Criterion.Operator.LTE, startTime));
            criteria.add(new Criterion("endTime", Criterion.Operator.GTE, endTime));
        }

        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }

        return nightPatrolService.getNpGroupView(criteria, pageRequest);
    }


    //s删除巡更组
    @DELETE
    @Path("delGroup/{groupId}")
    public Boolean deleteNpGroup(@PathParam("groupId") Long groupId) throws AppException {
        return nightPatrolService.deleteNpGroup(groupId);
    }

    //修改巡更组信息
    @PUT
    @Path("updateGroup/{groupId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean updateNpGroupInfo(@PathParam("groupId") Long groupId, NpGroupEntity npGroupEntity) throws AppException {
        return nightPatrolService.updateNpGroupInfo(groupId, npGroupEntity);
    }

    //删除巡更组成员
    @DELETE
    @Path("delPatrolManById/{groupId}/members/{memberId}")
    public Boolean deleteNpGroupMembers(@PathParam("groupId") Long groupId, @PathParam("memberId") Long memberId) throws AppException {

        return nightPatrolService.deleteNpGroupMembers(groupId, memberId);
    }

    //查询巡更记录
    @GET
    @Path("getRecords")
    public Page<NpResultEntity> getRcRecordPage(@QueryParam("startPatrolPlace") String startPatrolPlace,
                                                @QueryParam("patrolTime") String patrolTime,
                                                @QueryParam("groupName") String groupName,
                                                @BeanParam PageParam pageParam) {
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return nightPatrolService.getNpResultInfo(startPatrolPlace, patrolTime, groupName, pageParam, pageRequest);
    }


    //查询所有的巡更组巡更详情信息
    @GET
    @Path("getDetailByResultId/{resultId}")
    public Page<NpResultDetailEntity> getNpResultDetail(@PathParam("resultId") Long resultId,
                                                        @QueryParam("personnelName") String personnelName,
                                                        @QueryParam("type") int type,
                                                        @QueryParam("timeLength") Long timeLength,
                                                        @BeanParam PageParam pageParam) {
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return nightPatrolService.getNpResultDetailInfo(resultId, personnelName, type, timeLength, pageParam, pageRequest);
    }


}
